<?php
/**
 * Description of Model
 *
 * @author pp.pawlaczyk@gmail.com
 */
abstract class App_Model_Abstract {
    
    protected $_mapper = null;
    protected $_dbTable = null;
    
    public function __construct(array $options = null) {
        
        if(is_array($options)) {
            $this->setOptions($options);
        }
        
    }
    
    /**
     * Funckja ustawia atrybuty obiektu 
     * @param array $option
     * @return \App_Model_Abstract_Model
     */
    public function setOptions(array $options) {
        
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
        
    }
    
    public function __set($name, $value)
    {
        
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid property');
        }
        $this->$method($value);
        
        
    }
 
    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid property');
        }
        return $this->$method();
    }
    
    /**
     * Zwraca tablicę assocjacyjną wypełninych atrybutów (not null)
     * @return array
     */
    public function toArray() {
        
        $values = $this->_getArrayValues();
        $formArray = array();        
        
        foreach($values as $key => $val) {
            
            $keyName = null;
            
            if(!is_null($val)) {
                
                $keyName = substr($key, 1);
                
                $formArray[$keyName] = $val;                
            } 
            
        }
        return $formArray;
    }
    
    protected function _getArrayValues() {        
        $values = get_object_vars($this);        
        return $values;    
    }
    
}

?>
